package com.gxuwz.library_consumer.controller;

import com.github.pagehelper.PageInfo;
import com.gxuwz.commonmodule.entity.Book;
import com.gxuwz.commonmodule.entity.BorrowRecord;
import com.gxuwz.library_consumer.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    /**
     * 不同功能对应的视图文件名称,注意不能包含后缀名
     */
    private String view_main = "main";//图书主页
    private String view_list = "book/book_list";//管理员信息列表
    private String view_input = "book/book_add";//管理员读者信息
    private String view_edit = "book/book_edit";//编辑管理员信息
    private String view_edits = "book/book_edits";//编辑管理员信息
    private String redirect_list = "redirect:/book/findPage?pageIndex=1";
    private String redirect_borrowList = "redirect:/book/findPages?pageIndex=1";

    @Autowired
    private BookService bookService;


    /**
     * 图书首页
     * @return
     */
    @RequestMapping("/main")
    public String toAdminMain(){
        return view_main;
    }

    /**
     * 图书信息列表
     * @return
     */
    @GetMapping("/list")
    public ModelAndView listBook(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(view_list);
        List<Book> bookList = bookService.list(null);
        modelAndView.addObject("bookList",bookList);
        return modelAndView;
    }

    /**
     * 跳入增加图书视图
     * @return
     */
    @RequestMapping("/input")
    public String input(){
        return view_input;
    }

    /**
     * 增加图书信息
     * @param book
     * @return
     */
    @PostMapping("/add")
    public String add(Book book){
        bookService.save(book);
        return redirect_list;
    }

    /**
     * 通过ID 删除图书信息
     * @param bookID
     * @return
     */
    @GetMapping("/delete")
    public String delete(String bookID){
        bookService.deleteBook(bookID);
        System.out.println("删除成功");
        return redirect_list;
    }

    /**
     * 跳转到编辑图书视图
     * @param bookID
     * @param model
     * @return
     */
    @GetMapping("/get")
    public String get(String bookID, Model model){
        Book book = bookService.findById(bookID);
        model.addAttribute("m",book);
        return view_edit;
    }

    /**
     * 跳转到确认图书信息视图
     * @param bookID
     * @param model
     * @return
     */
    @GetMapping("/gets")
    public String gets(String bookID, Model model){
        Book book = bookService.findById(bookID);
        model.addAttribute("m",book);
        return view_edits;
    }

    /**
     * 编辑图书信息
     * @param book
     * @return
     */
    @PostMapping("/update")
    public String update(Book book){
        bookService.updateBook(book);
        return redirect_list;
    }

    /**
     * 分页查询（不带条件）
     * @param model
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public String findAll(Model model, @RequestParam(value = "pageIndex",defaultValue = "1")int pageIndex,
                          @RequestParam(value = "pageSize",defaultValue = "4",required = false)int pageSize){
        PageInfo page = bookService.findPage(pageIndex,pageSize);
        model.addAttribute("path","/book/findPage?pageIndex=");
        model.addAttribute("page",page);
        return view_list;
    }
}
